const session = require("../../utils/session.js");
const wxm = require("../../utils/wechat_modules.js");
const toast = require("../../utils/toasts.js");
const biz = require("../../utils/bizUtils.js");
const utils = require("../../utils/utils.js");
import {
  article_detail,
  op_logcom,
  timeline_detail
} from "../../utils/api.js";

const app = getApp();
const globalData = app.globalData;

Page({
  data: {
    is_logined: true,
    isIphoneX: globalData.isIphoneX,
  },
  onLoad: function(options) {

    if (options.type) {
      this.setData({
        type: options.type
      });
    }

    if (options.salesId) {
      this.setData({
        salesId: options.salesId
      });
    }

    if (options.from) {
      this.setData({
        from: options.from
      });
    }

    if (options.url) {
      this.setData({
        url: decodeURIComponent(options.url)
      });
    }

    if (options.tip) {
      this.setData({
        tip: options.tip
      });
    }

    if (options.path) {
      this.setData({
        path: decodeURIComponent(options.path)
      });
    }
  },
  onShow: function() {

    var that = this;
    var user = session.getUserSync();
    this.setData({
      isMy: (user.id == this.data.id),
      user: user
    });
  },
  // 获取当前用户信息
  getUserInfo: function() {
    session.getUser(this);
  },
  open: function() {
    var obj = this.data.obj;
    if (obj.path) {
      wx.navigateTo({
        url: obj.path,
      })
    }
  },
  onShareAppMessage: function(res) {
    this.opLog('SHARE');

    var that = this;
    var salesId = this.data.salesId;
    var type = this.data.type;
    var url = this.data.url;
    var tip = this.data.tip;
    var path = this.data.path;

    var query = "";
    if (salesId != null) {
      query = query + "salesId=" + salesId + "&";
    }
    if (type != null) {
      query = query + "type=" + type + "&";
    }
    if (type != null) {
      query = query + "tip=" + tip + "&";
    }
    if (url != null) {
      query = query + "url=" + encodeURIComponent(url) + "&";
    }
    if (path != null) {
      query = query + "path=" + encodeURIComponent(path) + "&";
    }

    var sourceType = 'USER';

    var path = '/pages/webview/webview?sourceType=' + sourceType + '&sourceId=' + this.data.user.id + '&from=share&' + query;

    console.log(path);
    return {
      path: path,
    }
  },
  opLog: function(type) {
    
    var that = this;
    if (that.data.obj.sellerId) {
      setTimeout(function () {
        op_logcom({
          objId: 1,
          sellerId: 1,
          type: type
        }).then(res => {
          // console.log(res);
        });
      }, 1000) //延迟时间 这里是1秒
    }
  },
  gotoPage:function() {
    biz.gotoPage(this.data.path);
  }
});